package com.lottery.im.util;

import android.app.Dialog;
import android.content.Context;
import android.widget.TextView;
import com.lottery.im.R;
import java.lang.ref.WeakReference;

/**
 * 进度条工具类
 */
public class ProgressUtils {
  public static Dialog progressDialog = null;
  private static TextView mTv;

  public static void dismissProgressDialog() {
    if (null != progressDialog && progressDialog.isShowing()) {
      progressDialog.dismiss();
    }
  }

  public static void showProgressDialog(Context context) {
    showProgressDialog(context, null);
  }

  public static void showProgressDialog(Context context, String message) {
    WeakReference<Context> reference = new WeakReference<>(context);
    //防止内存泄漏
    if (null != progressDialog && progressDialog.isShowing()) {
      return;
    }
    if (progressDialog == null || progressDialog.getContext() != context) {
      progressDialog = new Dialog(context, R.style.WinDialog);
      progressDialog.setOnDismissListener(dialog -> {
        progressDialog = null;
        mTv = null;
      });

      progressDialog.setContentView(R.layout.de_ui_dialog_loading);
      mTv = progressDialog.findViewById(R.id.message);
    }
    mTv.setText(message);
    progressDialog.show();
  }

  public static void releaseProgressDialog() {
    progressDialog = null;
  }
}
